package com.rd.project.controller;

import com.itextpdf.text.DocumentException;
import com.rd.project.model.Klas;
import com.rd.project.model.Score;
import com.rd.project.model.Student;
import com.rd.project.model.Test;
import com.rd.project.model.Vak;
import com.rd.project.service.DataService;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Stefan
 */
@ManagedBean(name="dataController")
@ViewScoped
public class DataController {
    
    private int selectedKlas = 0;
    private int selectedVak = 0;
    private int selectedTest = 0;
    private Test test = null;
    
    @EJB
    private DataService dataService;
    
    public List<Klas> getAlleKlassen() {
        return dataService.getAlleKlassen();
    }
    
    public List<Vak> getAlleVakkenFromKlas() {
        return dataService.getAlleVakkenFromKlas(this.selectedKlas);
    }
    
    public List<Test> getAlleTesten() {
        return dataService.getAlleTestenFromVak(this.selectedVak);
    }
    
    public List<Score> getAlleScores() {
        if(this.selectedTest != 0) {
           test = dataService.getTest(this.selectedTest);
        }
        return dataService.getAlleScoresFromTest(this.selectedTest);
    }

    public int getSelectedKlas() {
        return selectedKlas;
    }

    public void setSelectedKlas(int selectedKlas) {
        this.selectedKlas = selectedKlas;
    }

    public int getSelectedVak() {
        return selectedVak;
    }

    public void setSelectedVak(int selectedVak) {
        this.selectedVak = selectedVak;
    }

    public int getSelectedTest() {
        return selectedTest;
    }

    public void setSelectedTest(int selectedTest) {
        this.selectedTest = selectedTest;
    }

    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }
    
    public Student getStudent(int studentId)  {
        return dataService.getStudent(studentId);
    }
    
    public double getGemiddelde() {
        double gemiddelde = 0;

        for(int i = 0; i < getAlleScores().size(); i++) {
            gemiddelde += getAlleScores().get(i).getScore();
        }
        if(gemiddelde != 0) {           
            return ((gemiddelde / getAlleScores().size()) / test.getTotaal()) * 100;
        } else {
            return 0;
        }      
    }
    
    public void genereerPDFTest() 
            throws DocumentException, IOException {
        dataService.genereerPdfTest(selectedTest);
    }
    
    public void genereerPDFVak() 
            throws DocumentException, IOException {
        dataService.genereerPdfVak(selectedVak);
    }
    
    public void genereerPDFKlas() 
            throws DocumentException, IOException {
        dataService.genereerPdfKlas(selectedKlas);
    }
    
    private List<Score> scores;
    private Student student;

    @PostConstruct
    public void init() {
        scores = this.getScores();
        Map<String, String> params;
        params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String id = "0";
        if(params.containsKey("id")) {
            id = params.get("id");
            student = dataService.getStudent(Integer.parseInt(id));
        }
        
        
    }

  

    public List<Score> getScores() {
        return dataService.getAlleScore();
    }


    public List<Score> getScoreFromStudentId(int id) {
        return dataService.getScoreFromStudentId(id);
    }

    public List<Score> getScoreFromTestId(int id) {
        return dataService.getScoreFromTestId(id);
    }

    public int getTestIdFromScoreId(int id) {
        return dataService.getTestIdFromScoreId(id);
    }

    public Test getTestFromTestId(int id) {
        return dataService.getTestFromTestId(id);
    }

    public Test getTestFromScoreId(int scoreId) {
        return dataService.getTestFromScoreId(scoreId);
    }
    public Student getStudent(){
        return this.student;
    }
    public void setStudent(Student student){
        this.student = student;
    }      
    
    public Vak getVak(int vakId) {
        return dataService.getVak(vakId);
    }
    
    public Klas getKlasFromStudent() {
        return dataService.getKlasFromStudentId(this.student.getId().intValue());
    }
    
    public void genereerPDFStudent() 
            throws DocumentException, IOException {
        
        dataService.genereerPdfStudent(this.student.getId().intValue());
    }
}
